package com.wngbms;

import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.publisher.SynchronousSink;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Stream;

@Slf4j
public class TransformingDemo {

    public static void main(String[] args) {

        Flux<String> flux = Flux.just("123","123","123","123","123","123","123");
        Mono<String> mono = Mono.just("123");

        flux.map(x -> x);
        mono.map(x -> x);

        flux.handle(new BiConsumer<String, SynchronousSink<String>>() {
            @Override
            public void accept(String s, SynchronousSink<String> synchronousSink) {
                log.info("accept {}", s);
            }
        });

        Flux<Flux<String>> flux2 = Flux.just(Flux.just("123", "123"),
                Flux.just("123", "123"), Flux.just("123", "123"));

        flux2.flatMap(new Function<Flux<String>, Flux<String>>() {
            @Override
            public Flux<String> apply(Flux<String> stringFlux) {
                return stringFlux;
            }
        });

        flux2.flatMapSequential(new Function<Flux<String>, Flux<String>>() {
            @Override
            public Flux<String> apply(Flux<String> stringFlux) {
                return stringFlux;
            }
        });

//        mono.flatMapMany(new Function<String, Flux<Character>>() {
//            @Override
//            public Flux<Character> apply(String s) {
//                return Flux.fromIterable(Stream.of(s.toCharArray()).flatMap(new Function<char[], Stream<?>>() {
//                    @Override
//                    public Stream<?> apply(char[] chars) {
//                        return Arrays.stream(chars);
//                    }
//                }));
//            }
//        });

        flux2.startWith(flux);
        flux2.concatWithValues(flux);

        Mono<List<Flux<String>>> listMono = flux2.collectList();
        Mono<List<Flux<String>>> listMono1 = flux2.collectSortedList();



    }

}
